package com.zbycorp.generatecode.service.impl;

import com.intellij.database.psi.DbTable;
import com.intellij.openapi.components.State;
import com.intellij.openapi.components.Storage;
import com.intellij.openapi.project.Project;
import com.zbycorp.generatecode.dto.TableInfoSettingsDTO;
import com.zbycorp.generatecode.entity.TableInfo;
import com.zbycorp.generatecode.service.TableInfoSettingsService;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.Objects;

/**
 * @author YCL
 * @date 2023/3/27
 */
@State(name = "GenerateCodeSetting", storages = @Storage("generateCodeSetting.xml"))
public class TableInfoSettingsServiceImpl implements TableInfoSettingsService {

    private TableInfoSettingsDTO tableInfoSettings = new TableInfoSettingsDTO();

    @Override
    public @Nullable TableInfoSettingsDTO getState() {
        return tableInfoSettings;
    }

    @Override
    public void loadState(@NotNull TableInfoSettingsDTO state) {
        this.tableInfoSettings = state;
    }

    @Override
    public TableInfo getTableInfo(Project project, DbTable dbTable) {
        return Objects.requireNonNull(getState()).readTableInfo(project, dbTable);
    }
}
